package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Staff;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.query.StaffQueryObject;
import cn.wolfcode.crm.service.IStaffService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UploadUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletContext;

@Controller
@RequestMapping("staff")
public class StaffController {

    @Autowired
    private IStaffService staffService;
    @Autowired
    private ServletContext ctx;

    @RequestMapping("")
    @RequiresPermissions(value = {"职工首页","staff:index"} ,logical = Logical.OR)
    public String index(){

        return "staff/list";
    }

    @RequestMapping("list")
    @RequiresPermissions(value = {"职工列表","staff:list"} ,logical = Logical.OR)
    @ResponseBody
    public PageResult list(StaffQueryObject qo){
        PageResult result = staffService.query(qo);
        return result;
    }
    @RequestMapping("listData")
    @ResponseBody
    public Object listData(QueryObject qo){

        return staffService.list();
    }

    @RequestMapping("save")
    @RequiresPermissions(value = {"职工保存","staff:save"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult save(Staff entity, MultipartFile pic){
        JSONResult result = new JSONResult();
        if(entity.getId()!=null&&pic!=null&&pic.getSize()>0){
            UploadUtil.deleteFile(ctx,entity.getPortrait());
        }
        //保存图片
        if(pic != null && pic.getSize()>0){
            //保存到path
            String realPath = ctx.getRealPath("/upload");
            //保存图片
            String imagePath = UploadUtil.upload(pic,realPath);
            //保存到商品中
            entity.setPortrait(imagePath);
        }
        try{
            staffService.save(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }

    @RequestMapping("update")
    @RequiresPermissions(value = {"职工更新","staff:update"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult update(Staff entity){
        JSONResult result = new JSONResult();
        try{
            staffService.update(entity);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }


    @RequestMapping("delete")
    @RequiresPermissions(value = {"职工删除","staff:delete"} ,logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id){
        JSONResult result = new JSONResult();
        try{
            staffService.delete(id);
        }catch(Exception e){
            e.printStackTrace();
            result.remark("操作失败");
        }
        return result;
    }
}
